package pathfinders.mainloop;

import javax.swing.JButton;
import javax.swing.SwingUtilities;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.BorderFactory;

import pathfinders.dijkstra.GridGraph;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics; 
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseMotionAdapter;

public class MainLoop
{
    
	public static int windowWidth = 1024;
	public static int windowHeight = 768;
	
    public static void main(String[] args) 
    {
        SwingUtilities.invokeLater(new Runnable() 
        {
            public void run() 
            {
            	//System.out.println("Running");
                createAndShowGUI(); 
            }
        });
        //System.out.println("gui created");
    }


    private static void createAndShowGUI() 
    {
        //System.out.println("Created GUI on EDT? "+
        //SwingUtilities.isEventDispatchThread());
        
        JFrame f = new JFrame("PATHFINDERS");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        
        final MyPanel panel = new MyPanel();
        panel.setLayout(null);
        
        /*
        JButton testButton  = new JButton("Press");
        //testButton.setBounds(50,50,75,50);
        
        
        testButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                panel.testActionPerformed(e);
              }
            });
        
        panel.add(testButton);
        */
        
        f.add(panel);
        f.setSize(windowWidth,windowHeight);
        
        f.setVisible(true);
        
        
    }


}


class MyPanel extends JPanel implements Runnable
{
    
    public static int timeVar = 20;
    
    GridGraph grid;
    

    public MyPanel() 
    {

        setBorder(BorderFactory.createLineBorder(Color.black));

        /*
        addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
            	
            }
        });

        addMouseMotionListener(new MouseAdapter()
        {
            public void mouseDragged(MouseEvent e)
            {

            }
        });
		*/
        
        grid = new GridGraph(10,10,10);
        
        
        Thread th = new Thread(this);
        th.start();

    }
    
    public void testActionPerformed(ActionEvent e) 
    {

    }
    

    public Dimension getPreferredSize()
    {
        return new Dimension(250,200);
    }
    
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g); 
        grid.drawGraph(g);
        
        // MAIN PAINTING HAPPENS HERE
    }

	@Override
	public void run() 
	{
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
		while (true)
		{

			//repaint();

			try
		    {
		        Thread.sleep (timeVar);
		    }
		    catch (InterruptedException ex)
		    {
		    }
		    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		}	
		
	}  
}
